package www.notice.action;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import www.Action;
import www.ActionForward;
import www.notice.NoticeDTO;
import www.notice.NoticeMgr;
import www.utility.Utility;

public class NoticeList implements Action {
     @Override
    public ActionForward execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
         System.out.println("memberlist executed");
         
         request.setCharacterEncoding("euc-kr");
         
         NoticeMgr mgr = new NoticeMgr();

         JSONObject obj = new JSONObject(); 
         JSONArray data = new JSONArray();

         ArrayList<Object> list = mgr.list();
         Iterator<Object> iter = list.iterator();   
         
           if(list.size() >0){
               while(iter.hasNext() == true){
                     JSONObject item = new JSONObject();
                     NoticeDTO dto = (NoticeDTO) iter.next();
                     
                     item.put("noticeno", dto.getNoticeno());
                     item.put("nickname", Utility.toJS(dto.getNickname()));
                     item.put("title", Utility.toJS(dto.getTitle()));
                     item.put("contents", Utility.toJS(dto.getContents()));
                     item.put("profilephoto", dto.getProfilephoto());
                     item.put("noticedate", Utility.toJS(dto.getNoticedate()));
                     
                     data.add(item);
                  } 
                 obj.put("code","success");
                 obj.put("message", "데이터 목록 출력에 성공하였습니다.");
                 obj.put("data",data);   
               
           }else{
               obj.put("code","error");
               obj.put("message", "데이터 목록 출력에 실패하였습니다.");
           }
             
             System.out.print("Notice: " +  obj);
             request.setAttribute("obj", obj);
            
             ActionForward forward = new ActionForward();
             forward.setRedirect(false);  // 
             forward.setPath("/obj.jsp");
             
             return forward;
     }
}
